// pages/login/login.ts
import type { comanyList } from "../../type/type";
import { getCompanyList, login } from "../../api/auth/user";
import { resetGlobalState } from "mobx-miniprogram/lib/internal";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    companyList: [] as comanyList[],
    isPopup: false as Boolean,
    isLoading: false as Boolean,
    company: "" as String,
    companyid: -1 as Number,
    name: "" as String,
    pwd: "" as String,
    isPwd: false as Boolean,
    isName: false as Boolean,
    isCompanyid: false as Boolean,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.list();
  },
  list() {
    getCompanyList().then((res: any) => {
      this.setData({
        companyList: res.data,
      });
    });
  },
  handleInput() {
    this.setData({
      isPopup: true,
    });
  },
  handleClose() {
    this.setData({
      isPopup: false,
    });
  },
  handlePicker(e: any) {
    console.log(e);
    this.setData({
      company: e.detail.value.companyName,
      companyid: e.detail.value.id,
      isPopup: false,
    });
  },
  login() {
    let _this = this;
    let companyid = _this.data.companyid;
    let name = _this.data.name;
    let pwd = _this.data.pwd;
    _this.setData({
      isLoading: true,
    });
    if (!companyid) {
      this.setData({
        isCompanyid: true,
        isLoading: false,
      });
    }
    if (!name) {
      this.setData({
        isName: true,
        isLoading: false,
      });
    }
    if (!pwd) {
      this.setData({
        isPwd: true,
        isLoading: false,
      });
    }
    if (companyid && name && pwd) {
      try {
        wx.getUserProfile({
          desc: "用于获取用户微信个人信息",
          success: (res) => {
            // console.log(res);
            wx.login({
              success: (r) => {
                console.log(r);
                login(r.code, companyid, name, pwd).then((res: any) => {
                  //   console.log(res);
                  if (res.data.code == 200 || res.data.code == 201) {
                    _this.setData({
                      isLoading: false,
                    });
                    // wx.setStorageSync("token", res.data.data.token);
                    wx.setStorageSync("userInfo", res.data);
                    wx.showToast({
                      title: "登录成功",
                      icon: "success",
                      duration: 2000,
                      success: () => {
                        wx.switchTab({
                          url: "/pages/index/index",
                        });
                      },
                    });
                  } else {
                    _this.setData({
                      isLoading: false,
                    });
                    wx.showToast({
                      title: "登录失败",
                      icon: "error",
                      duration: 2000,
                    });
                  }
                }).catch((err: any) => {
                  _this.setData({
                    isLoading: false,
                  });
                });
              },
            });
          },
        });
      } catch {
        _this.setData({
          isLoading: false,
        });
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },
  /**
   * 监测更新
   */
  updata() {
    const updateManager = wx.getUpdateManager();
    // 请求完新版本信息的回调
    updateManager.onCheckForUpdate(res => {
      console.log(res)
      if (res.hasUpdate) {
        // 新版本下载成功
        updateManager.onUpdateReady(() => {
          wx.showModal({
            title: '更新提示',
            content: '新版本已经准备好，点击确定重启小程序',
            success(res) {
              if (res.confirm) {
                // 新的版本已经下载好，强制更新
                updateManager.applyUpdate()
              }
            }
          })
        })
      }
    })
    // 新版本下载失败
    updateManager.onUpdateFailed(res => {
      console.error(res)
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.updata();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
